package com.hmall.api.config;

import com.hmall.api.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
public class DefaultFeignConfig {
    //注册 ItemClientFallback
    @Bean
    public ItemClientFallback itemClientFallback() {
        return new ItemClientFallback();
    }
    //注册 CartClientFallback
    @Bean
    public CartClientFallback cartClientFallback(){
        return new CartClientFallback();
    }
    // 注册 TradeClientFallback
    @Bean
    public TradeClientFallback tradeClientFallback(){
        return new TradeClientFallback();
    }
    //注册 UserClientFallback
    @Bean
    public UserClientFallback userClientFallback(){
        return new UserClientFallback();
    }
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
    @Bean
    public RequestInterceptor feignRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if (userId!= null) {
                    requestTemplate.header("user-info", userId.toString());
                }

            }
        };
    }

    @Bean
    public PayClientFallback payClientFallback(){
        return new PayClientFallback();
    }

}
